package com.ty.user.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.util.IdUtil;
import com.ty.user.service.UserInfoService;
import com.ty.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@Controller
@RequestMapping("/user/info")
public class UserInfoController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private UserInfoService userInfoService;


    /**
     * 发送图形验证码给前端
     * @return
     */
    @GetMapping("/getCaptcha")
    @ResponseBody
    public R getCaptcha() {

        // 保存验证码信息
        String uuid = IdUtil.simpleUUID();

        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
//        //图形验证码写出，可以写出到文件，也可以写出到流
//        captcha.write("d:/circle.png");
        captcha.setBackground(Color.WHITE);

        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try {
            ImageIO.write(captcha.getImage(), "jpg", os);
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        redisTemplate.opsForValue().set("captcha_code:" + uuid, captcha.getCode(), 5, TimeUnit.MINUTES);

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("uuid", uuid);
        hashMap.put("captchaImg", Base64.encode(os.toByteArray()));

        return R.ok().data("data", hashMap);
    }

    /**
     * 校验图形验证码
     * @param map
     * @return
     */
    @PostMapping("/verifyCaptcha")
    @ResponseBody
    public R verifyCaptcha(@RequestBody HashMap<String, String> map) {
        String uuid = map.get("uuid");
        String captchaCode = map.get("captchaCode");

        Object redisCode = redisTemplate.opsForValue().get("captcha_code:" + uuid);
        redisTemplate.delete("captcha_code:" + uuid);

        if (captchaCode.equals(redisCode)) {
            return R.ok().data("code", "ok");
        }

        return R.ok().data("code", "error");
    }

    /**
     * 阿里云发送短信验证码
     * @param
     * @return
     */
    @PostMapping("/sendCode")
    @ResponseBody
    public R sendCode(@RequestBody HashMap<String, String> param) {
        boolean b = userInfoService.sendCode(param.get("phone"));

        if (b) {
            return R.ok().data("code", "ok");
        }

        return R.ok().data("code", "error");
    }


    /**
     * 手机号登录
     * @param map
     * @return
     */
    @PostMapping("/phoneLogin")
    @ResponseBody
    public R phoneLogin(@RequestBody HashMap<String, String> map) {

        String LoginType = map.get("showLoginType");
        if (LoginType.equals("phone")) {
            // 手机登录需要的参数
            String phone = map.get("phone");
            String smsCode = map.get("smsCode");
        } else {
            // 账号密码需要的参数
            String userName = map.get("userName");
            String password = map.get("userName");
        }

        userInfoService.phoneLogin(map);

        return R.ok();
    }

    @PostMapping("/accountLogin")
    @ResponseBody
    public R accountLogin(@RequestBody HashMap<String, String> map) {
        userInfoService.accountLogin(map);
        return R.ok();
    }

}
